﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Diagnostics;

namespace DataGridView
{
    public partial class DataGridView : Form
    {
        public DataGridView()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var ds = createDataSet();
            dgv1.DataSource = ds;
            dgv1.DataMember = "t_emp";

        }

        private string cnnStr = "Server=127.0.0.1,1433;User ID=sa;Pwd=colin;Initial Catalog=lhbdt;";
        private DataSet createDataSet()
        {
            using (SqlConnection cnn = new SqlConnection(cnnStr))
            {
                SqlDataAdapter adapter = new SqlDataAdapter("select FInterID,FBillNo,FDate from SEOrder", cnnStr);

                DataSet ds = new DataSet("erp");
                adapter.Fill(ds, "t_emp");

                /*
                    foreach(DataTable dt in ds.Tables)
                    {
                        Debug.WriteLine(dt.TableName); 
                    }
                */

                return ds;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            /*
                // datagridview 默认显示数组元素对象的第一个公共属性，也就是 Length 属性
                // 可以创建一个包装类, 只有一个属性, 显示 string 的值
                string[] stuff = new string[] { "Colin", "xxx", "AAA"};
                dgv1.DataSource = stuff;
            */
            Item[] stuff = new Item[] { new Item("Colin"), new Item("xxx"), new Item("AAA")};
            dgv1.DataSource = stuff;
        }


        private DataView dv;

        private void button3_Click(object sender, EventArgs e)
        {
            dv = new DataView(createDataSet().Tables["t_emp"]);
            dgv1.DataSource = dv;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            dv.RowFilter = "finterid = 1130 or finterid = 1131";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            dv.RowStateFilter = DataViewRowState.ModifiedCurrent;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            dv.Sort = "finterid desc, fbillno asc";
        }

        private void dgv1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
/*
            int i = 0;
            while (i < dgv1.Rows.Count)
            {
                if (aa == dgv1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString())
                {
                    this.dataGridView3.Rows[i].Cells[1].Style.BackColor = Color.MediumPurple;
                }
                i++;
            }
 */
        }

        private Color defaultBackColor;
        private Color defaultForeColor;
        private string currentCellValue;
        private Queue<Cell> oldPoints = new Queue<Cell>();

        private void dgv1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
/*
            if (defaultBackColor == null && dgv1.RowCount > 0)
            {
                defaultBackColor = dgv1.Rows[0].Cells[0].Style.BackColor;
                defaultForeColor = dgv1.Rows[0].Cells[0].Style.ForeColor;
            }

            currentCellValue = dgv1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();


            while(oldPoints.Count > 0)
            {
                Cell oldPoint = oldPoints.Dequeue();
                dgv1.Rows[oldPoint.RowIndex].Cells[oldPoint.ColumnIndex].Style.BackColor = defaultBackColor;
                dgv1.Rows[oldPoint.RowIndex].Cells[oldPoint.ColumnIndex].Style.ForeColor = defaultForeColor;
            }

            oldPoints.Enqueue(new Cell(){ ColumnIndex = e.ColumnIndex, RowIndex = e.RowIndex });
            dgv1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Black;
            dgv1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.White;

            for (int i = 0; i < dgv1.RowCount; i++)
            {
                if ( dgv1.Rows[i].Cells[e.ColumnIndex].Value != null && currentCellValue == dgv1.Rows[i].Cells[e.ColumnIndex].Value.ToString())
                {
                    oldPoints.Enqueue(new Cell() { ColumnIndex = e.ColumnIndex, RowIndex = i });
                    dgv1.Rows[i].Cells[e.ColumnIndex].Style.BackColor = Color.Black;
                    dgv1.Rows[i].Cells[e.ColumnIndex].Style.ForeColor = Color.White;
                }
            }
 */
        }

        /**
         * DataGridView
         *      .SelectionMode          选中模式
         *      .SelectedRows           选中的行, SelectionMode 必须是 FullRowSelect/ RowHeaderSelect 才有效
         * 
         * DataGridViewSelectionMode        DataGridView选定模式
         *      .CellSelect             单元格 
                .FullRowSelect          行  
                .FullColumnSelect       列 
                .RowHeaderSelect        行, 单元格
                .ColumnHeaderSelect     列, 单元格

         */
        private void btnGetSelected_Click(object sender, EventArgs e)
        {
            dgv1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            if (dgv1.SelectedRows.Count > 0)
            {
                MessageBox.Show(dgv1.SelectedRows[0].Cells["FBillNo"].Value.ToString());
            }
        }
    }

    internal struct Cell {
        public int ColumnIndex;
        public int RowIndex;
    }

    // 包装器类
    public class Item
    {
        private string _text;
        public Item(string text)
        {
            _text = text;
        }

        public string Text
        {
            get { return _text; }
        }

        public int TT
        {
            get { return 100; }
        }
    }
}
